﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace FjordagerGymnastikHvorBorMedlemmerne
{
    class KML
    {
   
        //this will be the contenitor 
        public XmlDocument doc = new XmlDocument();
        //this is Document element
        XmlNode DocumentNode;
        XmlNode kmlNode;
 
 
        //this function create the frame of the document, it is standard 
        public void GenerateKml()
        {
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);
            kmlNode = doc.CreateElement("kml");
            XmlAttribute xmlnsAttribute = doc.CreateAttribute("xmlns");
            xmlnsAttribute.Value = "http://earth.google.com/kml/2.1";
            kmlNode.Attributes.Append(xmlnsAttribute);
            doc.AppendChild(kmlNode);
            DocumentNode = doc.CreateElement("Document");
            kmlNode.AppendChild(DocumentNode);
        }
 
        //here we can save the document in a file, so it can be explored with google earth 
        public void SaveKml(){
            doc.Save(AppDomain.CurrentDomain.BaseDirectory +"name.kml");
        }
 
        //this function can be useful to add a style that can be called by other elements in the document 
        public void AddStyle(string name,string color)
        {
            XmlNode StyleNode = doc.CreateElement("Style");
            XmlAttribute styleAttribute = doc.CreateAttribute("id");
            styleAttribute.Value = name;
            StyleNode.Attributes.Append(styleAttribute);
            DocumentNode.AppendChild(StyleNode);
            XmlNode PolyStyleNode  = doc.CreateElement("PolyStyle");
            StyleNode.AppendChild(PolyStyleNode);
            XmlNode colorNode  = doc.CreateElement("color");
            colorNode.AppendChild(doc.CreateTextNode(color));
            XmlNode outlineNode = doc.CreateElement("outline");
            outlineNode.AppendChild(doc.CreateTextNode("0"));
            PolyStyleNode.AppendChild(colorNode);
            PolyStyleNode.AppendChild(outlineNode);

        
        } 
 
        //this function can add a point to the map, if you want extend functionalities you have to create other functions for each google earth shapes ( polygon, line, etc... ) 
        public void AddPoint(float lat,float lon,string name)
        {
            XmlNode PlacemarkNode = doc.CreateElement("Placemark");
            DocumentNode.AppendChild(PlacemarkNode);
            XmlNode nameNode = doc.CreateElement("name");
            nameNode.AppendChild(doc.CreateTextNode(name));
            PlacemarkNode.AppendChild(nameNode);
            XmlNode PointNode = doc.CreateElement("Point");
            PlacemarkNode.AppendChild(PointNode);
            XmlNode coordinateNode = doc.CreateElement("coordinates");
            PointNode.AppendChild(coordinateNode);
            coordinateNode.AppendChild(doc.CreateTextNode(lon.ToString(new System.Globalization.CultureInfo("en-US"))+","+lat.ToString(new System.Globalization.CultureInfo("en-US"))));
           
        }

        internal XmlDocument getKML()
        {
            return doc;            
        }
    }
} 
